<?php

@session_start();

require 'conf.php';

function error_msg($msg) 
{
    echo json_encode(array('error' => $msg));
}

function geteventtypes($sessiontoken)
{ 
    global $MW_host;
    $fields = array('USR_METADATA_REQ' => '',
                    'SESSION' => $sessiontoken);

    $fields = http_build_query($fields);

    $h = curl_init($MW_host); 
    curl_setopt($h, CURLOPT_POST, 1);
    curl_setopt($h, CURLOPT_POSTFIELDS, $fields); 
    curl_setopt($h, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($h, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($fields))); 

    $res = curl_exec($h);

    if(!mb_check_encoding($res, 'UTF-8'))
        $res = utf8_encode($res);
    $data = json_decode($res);
    return $data;
}

if (!isset($_SESSION['sessiontoken'])) {
    error_msg("Ogiltig session. Logga in igen");
} else { 
    $res = geteventtypes($_SESSION['sessiontoken']);
    if ($res === null) {
        error_msg("Ett fel uppstod i komunikationen med mellan-servern");
    } else {
        echo json_encode($res);
    }
}

?>
